﻿using System;
using System.Collections.Generic;
using Freemwork.Primitives.Graphic;
using Freemwork.Primitives.Input;
using Freemwork.Primitives.Input.Commands;
using Freemwork.Primitives.Input.Devices;
using Freemwork.Primitives.Math;
using Freemwork.Services;
using Freemwork.Services.File;
using Freemwork.Utilities;
using Freemwork.View.Strategies;
using Freemwork.World;
using Freemwork.World.Objects;
using Freemwork.World.Objects.Components.Debugging;
using Freemwork.World.Objects.Components.Graphics2D;
using Freemwork.World.Objects.Components.Input;

namespace Freemwork.Playstates
{
    public sealed class GamePlayState : PlayState
    {
        private Worldspawn worldspawn = new Worldspawn();

        public override Worldspawn Worldspawn { get { return worldspawn; } }
        public override string Identifier { get { return "Game"; } }

        public GamePlayState()
        {
            var controller = new CommandMap();
            controller["Up"] = new InputAction(
                            new KeyInputCommand(Key.Z, KeyInputCommandType.Down),
                            new XboxControllerJoystickInputCommand(0, ControllerJoystick.LS, Pos => Pos.Y > 0.2f));
            controller["Down"] = new InputAction(
                            new KeyInputCommand(Key.S, KeyInputCommandType.Down),
                            new XboxControllerJoystickInputCommand(0, ControllerJoystick.LS, Pos => Pos.Y < -0.2f));
            controller["Left"] = new InputAction(
                            new KeyInputCommand(Key.Q, KeyInputCommandType.Down),
                            new XboxControllerJoystickInputCommand(0, ControllerJoystick.LS, Pos => Pos.X < -0.2f));
            controller["Right"] = new InputAction(
                            new KeyInputCommand(Key.D, KeyInputCommandType.Down),
                            new XboxControllerJoystickInputCommand(0, ControllerJoystick.LS, Pos => Pos.X > 0.2f));

            var map = new GameObject();
            map.Components.Add(new Identity2D());
            map.Components.Add(new CompositeSpriteHolder("Tileset", Tools.GetTuplesFromT64(ServiceLocator.FileService.Load<String>("Map.t64", FileStorage.Project)), 48));
            map.QueryComponent<CompositeSpriteHolder>().MinDepth = 0f;
            map.QueryComponent<CompositeSpriteHolder>().MaxDepth = 1f;

            var hero = new GameObject();
            hero.Components.Add(new Identity2D(new Vector2(60, 60)));
            hero.Components.Add(new SpriteHolder("Tileset"));
            hero.Components.Add(new SpriteOscillator(new Size2D<int>(54, 78 - 4), new Size2D<int>(54, 78), 20f));
            hero.Components.Add(new BasicController2D(4.0f, controller) { Bobbing = true });
            hero.QueryComponent<SpriteHolder>().Sprite.Region = new Rectangle<int>(231 * 3, 99 * 3, 18 * 3, 26 * 3);
            hero.QueryComponent<SpriteHolder>().Sprite.Size = new Size2D<int>(54, 78);
            hero.QueryComponent<SpriteHolder>().Sprite.Origin = new Vector2(27, 78);
            hero.QueryComponent<SpriteHolder>().Sprite.Depth = 0.2f * 3 + 0.1F;

            Worldspawn.GameObjects.Add(0, map);
            Worldspawn.GameObjects.Add(1, hero);

            Worldspawn.Camera.ScrollingStrategy = new FollowCameraScrollingStrategy(hero);
        }
    }
}
